package com.fadadianqi.logistics.admin.controller;

import com.fadadianqi.commons.ParamHelper;
import com.fadadianqi.logistics.customer.model.Account;
import com.fadadianqi.logistics.customer.model.Customer;
import com.fadadianqi.logistics.customer.service.AccountService;
import com.fadadianqi.logistics.customer.service.CustomerService;
import com.fadadianqi.logistics.sell.model.Bill;
import com.fadadianqi.logistics.sell.model.Item;
import com.fadadianqi.logistics.sell.model.Payment;
import com.fadadianqi.logistics.sell.service.BillService;
import com.fadadianqi.logistics.sell.service.PaymentService;
import com.yeqiangwei.commons.util.DateTimeUtils;
import com.yeqiangwei.commons.util.Pagination;
import com.yeqiangwei.commons.util.ParamUtils;
import com.yeqiangwei.commons.util.ValidatorHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.*;


@Controller
public class CustomerBillController {

    @RequestMapping("/a/ajax/customer/bill/search")
    public String search(HttpServletRequest request, ModelMap modelMap) {
        String customerId = ParamUtils.getString(request, "cid", null);
        Customer customer = customerService.getById(customerId);
        modelMap.put("customer", customer);
        return "/admin/customer/billSearch";
    }

    @RequestMapping("/a/customer/bill/results")
    public String record(HttpServletRequest request, ModelMap modelMap) {
        String customerId = ParamUtils.getString(request, "cid", null);
        int m = ParamUtils.getInt(request, "m", 100);
        if (m > 1000) {
            m = 1000;
        }
        String pg = ParamUtils.getString(request, "pg", null);// 是否显示分页
        modelMap.put("pg", pg);

        String btime_ = DateTimeUtils.format(DateTimeUtils.getFirstDayOfMonth(), DateTimeUtils.FMT_YYYY_MM_DD);
        String etime_ = DateTimeUtils.format(DateTimeUtils.getLastDayOfMonth(), DateTimeUtils.FMT_YYYY_MM_DD);

        String _btime = ParamUtils.getString(request, "btime", btime_);
        String _etime = ParamUtils.getString(request, "etime", etime_);

        Date btime = ParamHelper.getBeginDayTimeByYYYYMMDD(_btime);
        Date etime = ParamHelper.getEndDayTimeByYYYYMMDD(_etime);

        Customer customer = customerService.getById(customerId);
        modelMap.put("customer", customer);

        Pagination pagination = new Pagination(request, m);
        List<Bill> list = billservice.between(btime, etime, customerId, pagination);
        pagination.setTotal(billservice.countBetween(btime, etime, customerId));
        List<CBillVO> vos = new ArrayList<CustomerBillController.CBillVO>();
        if (ValidatorHelper.isNotEmpty(list)) {
            for (Bill bill : list) {
                List<Account> accounts = accountService.findByBillId(bill);
                CBillVO vo = new CBillVO();
                vo.setAccounts(accounts);
                List<Payment> payments = paymentService.findByBillId(bill);
                vo.setPayments(payments);
                List<Item> items = billservice.findItemsByBillId(bill.getId());
                vo.setItems(items);
                vo.setCustomer(customer);
                vos.add(vo);
            }
        }
        Map<String, Object> eles = new HashMap<String, Object>();
        eles.put("cid", customerId);
        eles.put("m", m);
        eles.put("pg", pg);
        pagination.setElements(eles);
        modelMap.put("list", list);
        return "/admin/customer/billResults";
    }

    @Autowired
    private BillService billservice;

    @Autowired
    private AccountService accountService;

    @Autowired
    private PaymentService paymentService;

    @Autowired
    private CustomerService customerService;

    public static class CBillVO {

        private Customer customer;
        private Bill bill;
        private List<Item> items;
        private List<Payment> payments;
        private List<Account> accounts;

        public Customer getCustomer() {
            return customer;
        }

        public void setCustomer(Customer customer) {
            this.customer = customer;
        }

        public Bill getBill() {
            return bill;
        }

        public void setBill(Bill bill) {
            this.bill = bill;
        }

        public List<Item> getItems() {
            return items;
        }

        public void setItems(List<Item> items) {
            this.items = items;
        }

        public List<Payment> getPayments() {
            return payments;
        }

        public void setPayments(List<Payment> payments) {
            this.payments = payments;
        }

        public List<Account> getAccounts() {
            return accounts;
        }

        public void setAccounts(List<Account> accounts) {
            this.accounts = accounts;
        }
    }

}
